#!/usr/bin/python
#coding:utf-8
'''
pca主成成分分析
'''
import numpy as np
from sklearn.decomposition import PCA

x=np.array([[-1,-1],[-2,-1],[-3,-2],[1,1],[2,1],[3,2]])
pca=PCA(n_components='mle')  #mle自动确定保留特征数
pca.fit(x) 
#返回各自方差百分比，即单个变量方差贡献率
print(pca.explained_variance_ratio_)
#降维处理
z=pca.transform(x)
#恢复数据
ureduce=pca.components_  #得到降维用的Ureduce
x_rec = np.dot(z,ureduce)
print (x_rec)